package com.mes.basicdata.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.mes.basicdata.dao.iface.IDeptFunctionDao;
import com.mes.basicdata.domain.DeptFunction;
import com.mes.common.dao.BasicDao;

@Repository
public class DeptFunctionDao extends BasicDao<DeptFunction> implements IDeptFunctionDao {
	private final static Log log = LogFactory.getLog(DeptFunctionDao.class);

	/**
	 * 这样查效率非常一般，应该初始化时就将其读入 或者用spring 缓存机制 
	 * todo 
	 */
	@Override
	public String getCheckDutyDeptUid(String serviceDeptUid) {
		if (serviceDeptUid == null)
			return null;
		String deptUid = serviceDeptUid;
		List<DeptFunction> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DeptFunction.class);
			// 检验只设置了前四位
			crit.add(Restrictions.eq("serviceDeptUid", serviceDeptUid.substring(0, 4)));
			crit.add(Restrictions.eq("functionId", DeptFunction.FUNCTION_CHECK));
			list = crit.list();
			if (list != null && list.size() > 0)
				deptUid = list.get(0).getDeptUid();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return deptUid;
	}

	@Override
	public String getDeptUid(String serviceDeptUid, String functionId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getProductServiceDeptUid(String deptUid) {
		if (deptUid == null)
			return null;
		// 一般只对前四位限制
		// 避免专用，将这些逻辑都集中于此
		String serviceDeptUid = deptUid;
		if (deptUid.length() > 4)
			serviceDeptUid = deptUid.substring(0, 4);
		List<DeptFunction> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DeptFunction.class);
			// 检验只设置了前四位
			crit.add(Restrictions.eq("deptUid", serviceDeptUid));
			crit.add(Restrictions.eq("functionId", DeptFunction.FUNCTION_PRODUCT));
			list = crit.list();
			if (list != null && list.size() > 0)
				serviceDeptUid = list.get(0).getServiceDeptUid();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return serviceDeptUid;
	}

	@Override
	public String getDeviceServiceDeptUid(String deptUid) {
		if (deptUid == null)
			return null;
		// 一般只对前四位限制
		// 避免专用，将这些逻辑都集中于此
		String serviceDeptUid = deptUid;
		// if(deptUid.length()>4)serviceDeptUid=deptUid.substring(0,4);
		List<DeptFunction> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DeptFunction.class);
			// 检验只设置了前四位
			crit.add(Restrictions.eq("deptUid", serviceDeptUid));
			crit.add(Restrictions.eq("functionId", DeptFunction.FUNCTION_DEVICE));
			list = crit.list();
			if (list != null && list.size() > 0)
				serviceDeptUid = list.get(0).getServiceDeptUid();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return serviceDeptUid;
	}

	@Override
	public String getDeviceDutyDeptUid(String serviceDeptUid) {
		if (serviceDeptUid == null)
			return null;
		// 一般只对前四位限制
		// 避免专用，将这些逻辑都集中于此
		String dutyDeptUid = serviceDeptUid;
		if (dutyDeptUid.length() > 4)
			dutyDeptUid = dutyDeptUid.substring(0, 4);
		List<DeptFunction> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DeptFunction.class);
			// 检验只设置了前四位
			crit.add(Restrictions.like("serviceDeptUid", dutyDeptUid, MatchMode.START));
			crit.add(Restrictions.eq("functionId", DeptFunction.FUNCTION_DEVICE));
			list = crit.list();
			if (list != null && list.size() > 0)
				dutyDeptUid = list.get(0).getDeptUid();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dutyDeptUid;
	}

	// 2014.03.14 SHT
	@Override
	public String getTechDutyDeptUid(String serviceDeptUid) {
		if (serviceDeptUid == null)
			return null;
		// 一般只对前四位限制
		// 避免专用，将这些逻辑都集中于此
		String dutyDeptUid = serviceDeptUid;
		if (dutyDeptUid.length() > 4)
			dutyDeptUid = dutyDeptUid.substring(0, 4);
		List<DeptFunction> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DeptFunction.class);
			// 检验只设置了前四位
			crit.add(Restrictions.like("serviceDeptUid", dutyDeptUid, MatchMode.START));
			crit.add(Restrictions.eq("functionId", DeptFunction.FUNCTION_TECH));
			list = crit.list();
			if (list != null && list.size() > 0)
				dutyDeptUid = list.get(0).getDeptUid();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dutyDeptUid;
	}

	@Override
	public String getToolDutyDeptUid(String serviceDeptUid) {
		if (serviceDeptUid == null)
			return null;
		// 一般只对前四位限制
		// 避免专用，将这些逻辑都集中于此
		String dutyDeptUid = serviceDeptUid;
		if (dutyDeptUid.length() > 4)
			dutyDeptUid = dutyDeptUid.substring(0, 4);
		List<DeptFunction> list = null;
		try {
			Session session = this.getSession();
			Criteria crit = session.createCriteria(DeptFunction.class);
			// 检验只设置了前四位
			crit.add(Restrictions.like("serviceDeptUid", dutyDeptUid, MatchMode.START));
			crit.add(Restrictions.eq("functionId", DeptFunction.FUNCTION_TOOL));
			list = crit.list();
			if (list != null && list.size() > 0)
				dutyDeptUid = list.get(0).getDeptUid();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dutyDeptUid;
	}

}
